package cn.usian.service.empserviceimpl;

import cn.usian.mapper.EmpMapper;
import cn.usian.pojo.Emp;
import cn.usian.service.EmpService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Resource
    private EmpMapper empMapper;

    @Override
    public Page<Emp> findPage(String searchMap, Page<Emp> empPage) {
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        if(searchMap !=null && searchMap !=""){
            queryWrapper.like(Emp::getEname,searchMap);
        }
        queryWrapper.or();
        if(searchMap !=null && searchMap !=""){
            queryWrapper.like(Emp::getAddress,searchMap);
        }
        Page<Emp> page = empMapper.selectPage(empPage, queryWrapper);
        return page;
    }

    @Override
    public void add(Emp emp) {
        empMapper.insert(emp);
    }

    @Override
    public void update(Emp emp) {
        empMapper.updateById(emp);
    }

    @Override
    public void delete(Integer eid) {
        empMapper.deleteById(eid);
    }

    @Override
    public Emp findByEid(Integer eid) {
        return empMapper.selectById(eid);
    }

    @Override
    public List<Emp> findAll(String searchMap) {
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        if(searchMap !=null && searchMap !=""){
            queryWrapper.like(Emp::getEname,searchMap);
        }
        queryWrapper.or();
        if(searchMap !=null && searchMap !=""){
            queryWrapper.like(Emp::getAddress,searchMap);
        }
        List<Emp> list = empMapper.selectList(queryWrapper);
        return list;
    }
}
